package ca.camosun.inventory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.Context;

import ca.camosun.inventory.database.BoxAccessor;

public class Box implements Iterable<Item> {
	private List<Item> items = new ArrayList<Item>();
	private int id;
	private static int nextId = 0;
	private String boxName;
	private String qrLocationCode;
	public enum ItemList {
		ITEM_ADDED("Item has been added"),
		ITEM_UPDATED("Item has been updated"),
		UPDATE_FAILED("Failed to update item."),
		ADD_FAILED_DUPLICATE("Failed to add item: Duplicate Name"),
		ADD_FAILED_EMPTY("Failed to add item: Enter a name");

		
		private final String visible;
		
		private ItemList(String humanVisible) {
			visible = humanVisible;
		}
		
		@Override
		public String toString() {
			return visible;
		}
	}

	public Box(String inName, String inQrLocationCode, Context context) {
		items = new ArrayList<Item>();
		
		BoxAccessor boxDatabase = new BoxAccessor(context);
		boxDatabase.open();
		items = boxDatabase.retrieveBoxContents(inName);
		boxDatabase.close();

		id = nextId;
		nextId++;
		boxName = inName;
		qrLocationCode = inQrLocationCode;
	}
	
	public Box(String inName, Context context) {
		items = new ArrayList<Item>();
		
		BoxAccessor boxDatabase = new BoxAccessor(context);
		boxDatabase.open();
		items = boxDatabase.retrieveBoxContents(inName);
		boxDatabase.close();

		id = nextId;
		nextId++;
		boxName = inName;
	}

	public ItemList addItem(Item item, Context context) {
		int position = items.indexOf(item);
		if (position != -1) {
			return ItemList.ADD_FAILED_DUPLICATE;
		}
		
		if(item.getName().isEmpty()) {
			return ItemList.ADD_FAILED_EMPTY;
		}
		BoxAccessor boxDatabase = new BoxAccessor(context);
		boxDatabase.open();
		boxDatabase.addItem(boxName, item.getName(), item.getDescription(), 1);
		boxDatabase.close();
		
		items.add(item);
		return ItemList.ITEM_ADDED;
	}
	

	public ItemList updateItem(Item item, Item previousItem, Context context) {
		BoxAccessor boxDatabase = new BoxAccessor(context);
		boxDatabase.open();
		int position = items.indexOf(item);
		if (position != -1) {
			items.add(previousItem);
			boxDatabase.addItem(boxName, previousItem.getName(), previousItem.getDescription(), previousItem.getQuantity());
			boxDatabase.close();
			return ItemList.UPDATE_FAILED;
		}
	
		items.add(item);
		boxDatabase.addItem(boxName, item.getName(), item.getDescription(), item.getQuantity());
		boxDatabase.close();
		
		return ItemList.ITEM_UPDATED;
	}

	public void removeItem(Item item, Context context) {
		
		BoxAccessor boxDatabase = new BoxAccessor(context);
		boxDatabase.open();
		boxDatabase.deleteItem(boxName, item.getName());
		boxDatabase.close();
		
		items.remove(item);
	}

	public List<Item> getItems(Context context) {
		
		return items;
	}

	public List<String> getItemNames() {
		List<String> allItems = new ArrayList<String>();
		for (Item aItem : items) {
			allItems.add(aItem.getName());
		}
		return allItems;
	}
	
	
	public List<String> getItemQuantities() {
		List<String> allItems = new ArrayList<String>();
		for (Item aItem : items) {
			allItems.add(Integer.toString(aItem.getQuantity()));
		}
		return allItems;
	}

	public Item getOneItem(int index) {
		Item theGoods = (Item) items.get(index);
		return theGoods;
	}

	public Iterator<Item> iterator() {
		return items.iterator();
	}

	public int getId() {
		return id;
	}

	public Item searchForItem(Item looksLike) {
		Item foundItem = null;

		for (Item currentItem : items) {
			if (currentItem.equals(looksLike)) {
				foundItem = currentItem;
			}
		}

		if (foundItem == null) {
			foundItem = new Item("Error", " Item not found", 0);
		}

		return foundItem;
	}
	
	public String getCode() {
		return qrLocationCode;
	}
	
	public String getName() {
		return boxName;
	}

	public String toString() {
		return boxName;
	}


}
